package com.andatsoft.laisim.adapter;

import java.util.HashMap;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

import com.andatsoft.laisim.R;
import com.andatsoft.laisim.entity.FolderItem;
import com.andatsoft.laisim.theme.ThemeManager;
import com.andatsoft.laisim.utils.Common;
import com.andatsoft.laisim.utils.Utils;
import com.nineoldandroids.view.ViewHelper;

public class ScanFolderExpandableListAdapter extends BaseExpandableListAdapter {

	private Context mContext;
	private List<FolderItem> mGroupItems;
	private HashMap<String, List<FolderItem>> mChildItems;

	public interface onSavedFolderChangedListerner {
		public void onGroupFolderChanged(int groupIndex, boolean isSelect);
	}

	private onSavedFolderChangedListerner mOnSaveFolderChanged;

	public ScanFolderExpandableListAdapter(Context c, List<FolderItem> groups,
			HashMap<String, List<FolderItem>> childItems) {
		this.mContext = c;
		this.mGroupItems = groups;
		this.mChildItems = childItems;
	}

	@Override
	public Object getChild(int groupPos, int childPos) {
		return mChildItems.get(mGroupItems.get(groupPos).folderName).get(
				childPos);
	}

	@Override
	public long getChildId(int arg0, int childPos) {
		return childPos;
	}

	@SuppressLint("NewApi")
	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		FolderItem childContent = (FolderItem) getChild(groupPosition,
				childPosition);
		if (convertView == null) {
			LayoutInflater infalInflater = (LayoutInflater) this.mContext
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = infalInflater.inflate(
					R.layout.dialog_scan_folder_sub_item, parent, false);
		}
		TextView tv = (TextView) convertView
				.findViewById(R.id.tv_scan_folder_dialog_sub_item);

		if (childContent.isSelected) {
			if (Utils.androidOs > Common.ANDROID_OS_GINGERBREAD) {
				convertView.setAlpha(ThemeManager.getSelectedTheme()
						.getSelectedItemListAlpha());
			} else {
				ViewHelper.setAlpha(convertView, ThemeManager
						.getSelectedTheme().getSelectedItemListAlpha());
			}
		} else {
			if (Utils.androidOs > Common.ANDROID_OS_GINGERBREAD) {
				convertView.setAlpha(ThemeManager.getSelectedTheme()
						.getNormalItemListAlpha());
			} else {
				ViewHelper.setAlpha(convertView, ThemeManager
						.getSelectedTheme().getNormalItemListAlpha());
			}
		}
		tv.setText(childContent.folderName);
		return convertView;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return mChildItems.get(mGroupItems.get(groupPosition).folderName) == null ? 0
				: mChildItems.get(mGroupItems.get(groupPosition).folderName)
						.size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		return mGroupItems.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		return mGroupItems.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@SuppressLint("NewApi")
	@Override
	public View getGroupView(final int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		FolderItem groupItem = (FolderItem) getGroup(groupPosition);
		if (convertView == null) {
			LayoutInflater infalInflater = (LayoutInflater) this.mContext
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = infalInflater.inflate(
					R.layout.dialog_scan_folder_group_item, parent, false);
		}
		TextView tv = (TextView) convertView
				.findViewById(R.id.tv_scan_folder_dialog_group_item);
		final CheckBox chk = (CheckBox) convertView
				.findViewById(R.id.chkbox_scan_folder_dialog_group_item);

		chk.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (mOnSaveFolderChanged != null)
					mOnSaveFolderChanged.onGroupFolderChanged(groupPosition,
							chk.isChecked());
			}
		});

		if (groupItem == null)
			return convertView;
		if (groupItem.isSelected) {
			chk.setChecked(true);
			if (Utils.androidOs > Common.ANDROID_OS_GINGERBREAD) {
				convertView.setAlpha(ThemeManager.getSelectedTheme()
						.getSelectedItemListAlpha());
			} else {
				ViewHelper.setAlpha(convertView, ThemeManager
						.getSelectedTheme().getSelectedItemListAlpha());
			}
		} else {
			chk.setChecked(false);
			if (Utils.androidOs > Common.ANDROID_OS_GINGERBREAD) {
				convertView.setAlpha(ThemeManager.getSelectedTheme()
						.getNormalItemListAlpha());
			} else {
				ViewHelper.setAlpha(convertView, ThemeManager
						.getSelectedTheme().getNormalItemListAlpha());
			}
		}
		tv.setText(groupItem.folderName);
		return convertView;
	}

	@Override
	public boolean hasStableIds() {
		return false;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

	public void setOnSavedFolderGroupChangedListener(
			onSavedFolderChangedListerner event) {
		this.mOnSaveFolderChanged = event;
	}

}
